//
//  XYOriginalStatusView.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/31.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit
import SnapKit

class XYOriginalStatusView: UIView {
    
    private var bottomConstraint:Constraint?
    
    
    
    var statusViewModel: XYStatusViewModel? {
        
        didSet {
            self.bottomConstraint?.uninstall()
            if let pictures = statusViewModel?.status?.pic_urls where pictures.count > 0 {
                pictureView.hidden = false
                pictureView.pictures = pictures
                pictureView.viewModel = statusViewModel
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(XYStatusCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(textLabel).offset(XYStatusCellMargin).constraint
                })
            }
            
            // 设置数据
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 设置头像
            if let iconUrl = statusViewModel?.status?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: iconUrl), placeholderImage: UIImage(named: "avatar_default_small"))
            }
            // 设置会员图标
            memberView.image = statusViewModel?.memberImage
            // 设置认证图标
            verifiedView.image = statusViewModel?.verifiedImage
            // 设置 来源
            sourceLabel.text = statusViewModel?.sourceText
            // 设置时间
            timeLabel.text = statusViewModel?.timeText
            // 设置正文
            textLabel.attributeString = statusViewModel?.originalStatusAttributeText
            textLabel.matchResults = statusViewModel?.originalMatchResults
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

  
    private func setUpView() {
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(sourceLabel)
        addSubview(timeLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(textLabel)
        addSubview(pictureView)
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(XYStatusCellMargin)
            make.leading.equalTo(self).offset(XYStatusCellMargin)
            make.size.equalTo(CGSizeMake(XYStatusCellIconViewWH, XYStatusCellIconViewWH))
        }
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-3)
            make.centerY.equalTo(iconView.snp_bottom).offset(-3)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(XYStatusCellMargin)
        }
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(XYStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(XYStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        // 内容
        textLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(XYStatusCellMargin)
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -XYStatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(XYStatusCellMargin)
            make.leading.equalTo(textLabel)
            
        }
        // 约束原创微博的底部 
        self.snp_makeConstraints { (make) -> Void in
           bottomConstraint = make.bottom.equalTo(pictureView).offset(XYStatusCellMargin).constraint
        }
        
    }
    // 懒加载控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"avatar_default_small"))
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 10)
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    // 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    // 认证图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 内容
    private lazy var textLabel: XYStatusContentLabel = {
        
        let textLabel = XYStatusContentLabel()
        textLabel.font = UIFont.systemFontOfSize(XYStatusCellFontSize)
        return textLabel
    }()
    private lazy var pictureView: XYHomePictureView = {
        
        let pictureView = XYHomePictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
    
    
}
